// (C) Andreas Kaiser 2008

#ifndef _RF01_H
#define _RF01_H

//-----------------------------------------------------------------------------
// RF12 definitions

#define RF01CFG				0x8000
#define RF01CFG_Frequency315		(0<<11)
#define RF01CFG_Frequency433		(1<<11)
#define RF01CFG_Frequency868		(2<<11)
#define RF01CFG_Frequency915		(3<<11)
#define RF01CFG_EnableBatteryDetector	(1<<10)
#define RF01CFG_EnableWakeupTimer	(1<<9)
#define RF01CFG_EnableCrystal		(1<<8)
#define RF01CFG_CrystalLoad(pF)		((int)(((pF)-8.5)*2)<<4)
#define RF01CFG_Bandwidth400		(1<<1)
#define RF01CFG_Bandwidth340		(2<<1)
#define RF01CFG_Bandwidth270		(3<<1)
#define RF01CFG_Bandwidth200		(4<<1)
#define RF01CFG_Bandwidth134		(5<<1)
#define RF01CFG_Bandwidth67		(6<<1)
#define RF01CFG_DisableClockOutput	(1<<0)

#define RF01FREQ			0xA000
#define RF01FREQ_Calc(MHz,c1,c2)	((int)(((400.0*(MHz))/c1-4000.0*c2)))
#define RF01FREQ_315(MHz)		RF01FREQ_Calc(MHz,1,31)
#define RF01FREQ_433(MHz)		RF01FREQ_Calc(MHz,1,43)
#define RF01FREQ_868(MHz)		RF01FREQ_Calc(MHz,2,43)
#define RF01FREQ_915(MHz)		RF01FREQ_Calc(MHz,3,30)

#define RF01RCTL			0xC000
#define RF01RCTL_VDI_DRSSI		(0<<6)
#define RF01RCTL_VDI_DQD		(1<<6)
#define RF01RCTL_VDI_CRL		(2<<6)
#define RF01RCTL_VDI_Always		(3<<6)
#define RF01RCTL_Gain0			(0<<4)
#define RF01RCTL_Gain6			(2<<4)
#define RF01RCTL_Gain14			(1<<4)
#define RF01RCTL_Gain20			(3<<4)
#define RF01RCTL_RSSI103		(0<<1)
#define RF01RCTL_RSSI97			(1<<1)
#define RF01RCTL_RSSI91			(2<<1)
#define RF01RCTL_RSSI85			(3<<1)
#define RF01RCTL_RSSI79			(4<<1)
#define RF01RCTL_RSSI73			(5<<1)
#define RF01RCTL_RSSI67			(6<<1)
#define RF01RCTL_RSSI61			(7<<1)
#define RF01RCTL_DisableRX		(0<<0)
#define RF01RCTL_EnableRX		(1<<0)

#define RF01WAKE			0xE000
#define RF01WAKE_Prescaler(n)		(((n)&0x1F)<<8)			/* 0..31 */
#define RF01WAKE_Timer(ms)		((ms)&0xFF)			/* 0..255 */

#define RF01DUTY			0xCC00
#define RF01DUTY_Enable			(1<<0)
#define RF01DUTY_Timer(n)		((n)&0xFE)			/* 0..RF01WAKE_Timer */

#define RF01LBCLK			0xC200
#define RF01LBCLK_Clock1000KHz		(0<<5)
#define RF01LBCLK_Clock1250KHz		(1<<5)
#define RF01LBCLK_Clock1666KHz		(2<<5)
#define RF01LBCLK_Clock2000KHz		(3<<5)
#define RF01LBCLK_Clock2500KHz		(4<<5)
#define RF01LBCLK_Clock3333KHz		(5<<5)
#define RF01LBCLK_Clock5000KHz		(6<<5)
#define RF01LBCLK_Clock10000KHz		(7<<5)
#define RF01LBCLK_Voltage(V)		(((int)((V)-2.2)*10)&0x1F)	/* 2.2..5.4V */

#define RF01AFC				0xC600
#define RF01AFC_Off			(0<<6)
#define RF01AFC_Once			(1<<6)
#define RF01AFC_WhenReceiving		(2<<6)
#define RF01AFC_Always			(3<<6)
#define RF01AFC_NoLimit			(0<<4)
#define RF01AFC_Wide			(1<<4)
#define RF01AFC_Medium			(2<<4)
#define RF01AFC_Narrow			(3<<4)
#define RF01AFC_Strobe			(1<<3)
#define RF01AFC_FineMode		(1<<2)
#define RF01AFC_EnableOffsetRegister	(1<<1)
#define RF01AFC_EnableOffsetCalculation	(1<<0)

#define RF01FILT			0xC420
#define RF01FILT_AutoLock		(1<<7)
#define RF01FILT_SlowClockRecovery	(0<<6)
#define RF01FILT_FastClockRecovery	(1<<6)
#define RF01FILT_DigitalFilter		(1<<3)
#define RF01FILT_AnalogFilter		(2<<3)
#define RF01FILT_DQD(n)			((n)&7)

/* slow: 600..38400, fast: 9600..115200 */
#define RF01RATE(bps)			(0xC800 | ((bps)<9600 \
					 ? ((1<<7)|((int)(10e6/29/8/(bps)-0.5))) \
					 : ((0<<7)|((int)(10e6/29/(bps)-0.5)))))

#define RF01FIFO			0xCE00
#define RF01FIFO_Level(n)		(((n)&15)<<4)
#define RF01FIFO_VDI			(0<<2)
#define RF01FIFO_SyncPattern		(1<<2)
#define RF01FIFO_VDIandSync		(2<<2)
#define RF01FIFO_Always			(3<<2)
#define RF01FIFO_EnableAfterSync	(1<<1)
#define RF01FIFO_EnableFIFO		(1<<0)

#define RF01STAT			0x0000
// Interrupt:
#define RF01STAT_FFIT			(1<<15)	// number of data bits in RX FIFO has reached limit
#define RF01STAT_FFOV			(1<<14)	// Rx FIFO overflow
#define RF01STAT_WKUP			(1<<13)	// wakup timer overflow
#define RF01STAT_LBD			(1<<12)	// low battery detect
// Status_
#define RF01STAT_FFEM			(1<<11)	// FIFO empty
#define RF01STAT_DRSSI			(1<<10)	// strength of incoming signal is strong enough
#define RF01STAT_DQD			(1<<9)	// data quality detector output
#define RF01STAT_CRL			(1<<8)	// clock recovery locked
#define RF01STAT_ATGL			(1<<7)	// toggling in each AFC cycle
#define RF01STAT_ASAME			(1<<6)	// AFC stabilized
#define RF01STAT_OFFS(x)		(((x)&0x000F)|((x)&0x10)?~0x000F:0)

#endif //_RFM01_H
